package com.yanqu.road.dao.impl.monopolymarket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.monopolymarket.group.MonopolyMarketBackEndGroup;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/12 15:59
 */
public class MonopolyMarketBackEndGroupDaoImpl extends TempDao {

    public List<MonopolyMarketBackEndGroup> getMonopolyMarketBackEndGroupList(int channelId){
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_monopolymarket_backend_group where `channelId`=? ";
        List list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1,channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parseData(rs));
            }
            rs = call.executeQuery();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    protected MonopolyMarketBackEndGroup parseData(ResultSet rs) throws SQLException {
        MonopolyMarketBackEndGroup monopolyMarketBackEndGroup = new MonopolyMarketBackEndGroup();
        monopolyMarketBackEndGroup.setChannelId(rs.getInt("channelId"));
        monopolyMarketBackEndGroup.setGroupId(rs.getLong("groupId"));
        monopolyMarketBackEndGroup.setServerList(JSON.parseObject(rs.getString("serverList"), new TypeReference<java.util.List<java.lang.Long>>() {}));
        monopolyMarketBackEndGroup.setState(rs.getInt("state"));
        monopolyMarketBackEndGroup.setFortressState(rs.getInt("fortressState"));
        monopolyMarketBackEndGroup.setChangTime(rs.getInt("changTime"));

        return monopolyMarketBackEndGroup;
    }
}
